/*  JInternalFrameSortearCampeonato.java
 *  Copyright (C) 2009  Tiago Silva Loureiro
 *  This File is part of Bier Manager.
 *  Bier Manager a is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*  JInternalFrameSortearCampeonato.java - Bier Manager
 *
 *  Realiza o sorteio dos campeonatos da temporada de uma forma
 *  animada ao usuário.
 *
 *  @author Tiago Silva Loureiro
 * *
 */
package biermanager.interfaceGrafica;

import biermanager.nucleo.Campeonato;
import biermanager.nucleo.Temporada;
import biermanager.nucleo.Time;
import biermanager.componentes.PintorDeBackground;
import biermanager.nucleo.Sorteio;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.BorderFactory;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.JXLabel;


public class JInternalFrameSortearCampeonato extends javax.swing.JInternalFrame {
    private ArrayList<Time> times;
    private float alpha;
    private PintorDeBackground pintor;
    private DefaultTableModel modeloTabelaSorteio;
    private Temporada temporada;

    private void setAlpha(){        
        jxPainelEstruturado.setAlpha(alpha);
    }
    /** Creates new form JInternalFrameSortearCampeonato
     * @param times
     * @param indiceTimeAtual 
     */
    public JInternalFrameSortearCampeonato(ArrayList<Time> times, int indiceTimeAtual) {
        initComponents();
        iniciarDisplay();
        iniciarBackgrounds();
        iniciarTabela(times, indiceTimeAtual);



    }
    private void iniciarDisplay(){
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation( (int)( ( screenSize.getWidth() - this.getWidth() ) / 2 ), 10 );
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
    }

    private void iniciarBackgrounds(){

        pintor = new PintorDeBackground();
        jxPainelEstruturado.setBackgroundPainter(pintor.pintarDegradePreto(jxPainelEstruturado));
        jxlabelBotaoContinuar.setBackgroundPainter(pintor.pintarBotaoSemFocoCapsulaPreto());
        jxlabelBotaoFechar.setBackgroundPainter(pintor.pintarCinzaTransparente(jxlabelBotaoFechar));
    }

    private void iniciarTabela(ArrayList<Time> times,int indiceTimeAtual) {
        Campeonato cmp = Sorteio.SortearCampeonato(times);
        temporada = new Temporada();
        temporada.addCompeticao(cmp);
    }

 /*   public class RenderizadorDeRodadas extends JXPanel implements TableCellRenderer {
    private Painter backgroundPainter(Time timesDaRodada ){
        File f= new File(timesDaRodada.getBandeira());
        BufferedImage imagemTime1 = null;
            try {
                imagemTime1 = ImageIO.read(f);
            } catch (IOException ex) {
                Logger.getLogger(JInternalFrameSortearCampeonato.class.getName()).log(Level.SEVERE, null, ex);
            }
        BufferedImage mask = new BufferedImage(15,15 ,BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = mask.createGraphics();
        g2d.drawImage(imagemTime1, null, 2, 2);
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, .3f));
        g2d.setColor(Color.lightGray);
        g2d.fillRect(0, 0, 15, 15);
        g2d.dispose();
        for (int x = 0; x < 15; x++) {
            for (int y = 0; y < 15; y++) {
                int argb = imagemTime1.getRGB(x, y);
                argb = (int) ((argb >> 24 & 0xFF) * .3f) << 24 |
                        Color.lightGray.getRGB() & 0x00FFFFFF;
                mask.setRGB(x, y, argb);
            }
        }
        ImagePainter maskp = new ImagePainter(mask);
        maskp.setHorizontalAlignment(HorizontalAlignment.LEFT);
        maskp.setAntialiasing(true);
        
       
        Image imagem = imagemTime1.getScaledInstance(30, 30, Image.SCALE_SMOOTH);
        BufferedImage bimage = new BufferedImage(imagem.getWidth(null), imagem.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics g = bimage.createGraphics();
        g.drawImage(imagem, 0, 0, null);
        g.dispose();        


        ImagePainter img = new ImagePainter(bimage);
        img.setHorizontalAlignment(HorizontalAlignment.LEFT);
        img.setAntialiasing(true);
        Insets is = img.getInsets();
        Font fonte = null;
        try {
            fonte = Font.createFont(Font.TRUETYPE_FONT, getClass().getClassLoader().getResourceAsStream("./biermanager/fontes/rabiscos.ttf"));
        } catch (FontFormatException ex) {
            Logger.getLogger(Font.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Font.class.getName()).log(Level.SEVERE, null, ex);
        }
        GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(fonte);
        String titulo = timesDaRodada.getNomeAbreviado() + " x ";
        TextPainter tx = new TextPainter(titulo, new Font(fonte.getFontName(), Font.PLAIN, 15));
        tx.setFillPaint(new Color(255,255,255));
        tx.setHorizontalAlignment(HorizontalAlignment.CENTER);
        tx.setVerticalAlignment(VerticalAlignment.CENTER);
        TextPainter tx2 = new TextPainter(titulo, new Font(fonte.getFontName(), Font.PLAIN, 15));
        tx2.setFillPaint(new Color(50,50,50,128));
        tx2.setHorizontalAlignment(HorizontalAlignment.CENTER);
        tx2.setVerticalAlignment(VerticalAlignment.CENTER);
        is = tx.getInsets();
        tx2.setInsets(new Insets(is.top + 4, is.left + 3, is.bottom, is.right));
        //CompoundPainter cp = new CompoundPainter(maskp,maskp2,img,img2,tx2,tx);
        MattePainter mp = new MattePainter(new Color(0,0,0));
        CompoundPainter cp = new CompoundPainter(mp,img,tx2,tx);
        return ( cp );
    }

        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            Time timesDaRodada = (Time) value;
            this.setBackgroundPainter(backgroundPainter(timesDaRodada));
            this.setOpaque(true);
            return this;
        }


}*/
      
    /**
     * Gerado automaticamente pelo matisse( Editor visual do NetBeans ).
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jxPainelInvisivel = new org.jdesktop.swingx.JXPanel();
        jxPainelEstruturado = new org.jdesktop.swingx.JXPanel();
        jxPainelDaTabela = new org.jdesktop.swingx.JXPanel();
        jxlabelBotaoContinuar = new org.jdesktop.swingx.JXLabel();
        jxlabelBotaoFechar = new org.jdesktop.swingx.JXLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), javax.swing.BorderFactory.createLineBorder(new java.awt.Color(122, 122, 122))));
        setFocusCycleRoot(false);
        setFocusable(false);
        setPreferredSize(new java.awt.Dimension(558, 476));
        setRequestFocusEnabled(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                formComponentAdded(evt);
            }
        });

        jxPainelInvisivel.setOpaque(false);
        jxPainelInvisivel.setDoubleBuffered(false);

        jxPainelEstruturado.setAlpha(0.f);
        jxPainelEstruturado.setBackground(new java.awt.Color(255, 255, 255));
        jxPainelEstruturado.setInheritAlpha(false);
        jxPainelEstruturado.setOpaque(false);
        jxPainelEstruturado.setDoubleBuffered(false);

        jxPainelDaTabela.setInheritAlpha(false);
        jxPainelDaTabela.setOpaque(false);
        jxPainelDaTabela.setDoubleBuffered(false);

        jxlabelBotaoContinuar.setForeground(new java.awt.Color(255, 255, 255));
        jxlabelBotaoContinuar.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jxlabelBotaoContinuar.setText("Continuar!");
        jxlabelBotaoContinuar.setFont(new java.awt.Font("Tahoma", 1, 12));
        jxlabelBotaoContinuar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jxlabelBotaoContinuarMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jxlabelBotaoContinuarMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jxlabelBotaoContinuarMouseExited(evt);
            }
        });

        javax.swing.GroupLayout jxPainelDaTabelaLayout = new javax.swing.GroupLayout(jxPainelDaTabela);
        jxPainelDaTabela.setLayout(jxPainelDaTabelaLayout);
        jxPainelDaTabelaLayout.setHorizontalGroup(
            jxPainelDaTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jxPainelDaTabelaLayout.createSequentialGroup()
                .addContainerGap(366, Short.MAX_VALUE)
                .addComponent(jxlabelBotaoContinuar, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(52, 52, 52))
        );
        jxPainelDaTabelaLayout.setVerticalGroup(
            jxPainelDaTabelaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jxPainelDaTabelaLayout.createSequentialGroup()
                .addGap(315, 315, 315)
                .addComponent(jxlabelBotaoContinuar, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                .addGap(81, 81, 81))
        );

        javax.swing.GroupLayout jxPainelEstruturadoLayout = new javax.swing.GroupLayout(jxPainelEstruturado);
        jxPainelEstruturado.setLayout(jxPainelEstruturadoLayout);
        jxPainelEstruturadoLayout.setHorizontalGroup(
            jxPainelEstruturadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxPainelEstruturadoLayout.createSequentialGroup()
                .addComponent(jxPainelDaTabela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jxPainelEstruturadoLayout.setVerticalGroup(
            jxPainelEstruturadoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxPainelEstruturadoLayout.createSequentialGroup()
                .addContainerGap(43, Short.MAX_VALUE)
                .addComponent(jxPainelDaTabela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jxPainelInvisivelLayout = new javax.swing.GroupLayout(jxPainelInvisivel);
        jxPainelInvisivel.setLayout(jxPainelInvisivelLayout);
        jxPainelInvisivelLayout.setHorizontalGroup(
            jxPainelInvisivelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jxPainelInvisivelLayout.createSequentialGroup()
                .addComponent(jxPainelEstruturado, javax.swing.GroupLayout.PREFERRED_SIZE, 548, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jxPainelInvisivelLayout.setVerticalGroup(
            jxPainelInvisivelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jxPainelEstruturado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jxlabelBotaoFechar.setForeground(new java.awt.Color(255, 255, 255));
        jxlabelBotaoFechar.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jxlabelBotaoFechar.setText("x");
        jxlabelBotaoFechar.setFont(new java.awt.Font("Tahoma", 1, 14));
        jxlabelBotaoFechar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jxlabelBotaoFechar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jxlabelBotaoFecharMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jxlabelBotaoFecharMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jxlabelBotaoFecharMouseExited(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jxPainelInvisivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jxlabelBotaoFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jxlabelBotaoFechar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jxPainelInvisivel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
      
         jxPainelEstruturado.setAlpha(0.f);
            alpha = 0.f;
        final Timer tempo = new Timer();        //Temporizador
        TimerTask tarefa = new TimerTask(){     //Objeto tarefa a ser realizada
            public void run() {                 //Metodo run que implementa a tarefa
                if( ( alpha + 0.05 ) <= 1.0f)
                    alpha = alpha + 0.05f;
                else{
                    tempo.cancel();
                }
                setAlpha();
            }};
    tempo.schedule(tarefa, 50, 5);
    }//GEN-LAST:event_formComponentShown

    private void formComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_formComponentAdded
        
    }//GEN-LAST:event_formComponentAdded

    private void jxlabelBotaoContinuarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoContinuarMouseClicked
    firePropertyChange("Iniciar Jogo", null, temporada);
}//GEN-LAST:event_jxlabelBotaoContinuarMouseClicked

    private void jxlabelBotaoContinuarMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoContinuarMouseEntered
        JXLabel jxl = (JXLabel) evt.getSource();
        jxl.setBackgroundPainter(pintor.pintarBotaoComFocoCapsulaPreto());
        jxl.setForeground(new Color(255, 255, 255));
}//GEN-LAST:event_jxlabelBotaoContinuarMouseEntered

    private void jxlabelBotaoContinuarMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoContinuarMouseExited
        JXLabel jxl = (JXLabel) evt.getSource();
        jxl.setBackgroundPainter(pintor.pintarBotaoSemFocoCapsulaPreto());
        jxl.setForeground(new Color(50, 50, 50));
}//GEN-LAST:event_jxlabelBotaoContinuarMouseExited

    private void jxlabelBotaoFecharMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoFecharMouseClicked
        firePropertyChange("Fechar", null, null);
}//GEN-LAST:event_jxlabelBotaoFecharMouseClicked

    private void jxlabelBotaoFecharMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoFecharMouseEntered
        JXLabel jxl = (JXLabel) evt.getSource();
        jxl.setForeground(new Color(200, 10, 10));
        jxl.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
}//GEN-LAST:event_jxlabelBotaoFecharMouseEntered

    private void jxlabelBotaoFecharMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jxlabelBotaoFecharMouseExited
        JXLabel jxl = (JXLabel) evt.getSource();
        jxl.setForeground(new Color(255, 255, 255));
        jxl.setBorder(null);
}//GEN-LAST:event_jxlabelBotaoFecharMouseExited

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXPanel jxPainelDaTabela;
    private org.jdesktop.swingx.JXPanel jxPainelEstruturado;
    private org.jdesktop.swingx.JXPanel jxPainelInvisivel;
    private org.jdesktop.swingx.JXLabel jxlabelBotaoContinuar;
    private org.jdesktop.swingx.JXLabel jxlabelBotaoFechar;
    // End of variables declaration//GEN-END:variables
}
